package com.mxx.common.thread;


import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 线程工厂
 */

public class DefaultThreadFactory implements ThreadFactory {

    private String threadName;
    private AtomicLong count;

    public DefaultThreadFactory() {
        this("DefaultThreadName");
    }

    public DefaultThreadFactory(String threadName) {
        this.threadName = threadName;
        count = new AtomicLong(0);
    }

    public String nextThreadName() {
        return threadName + "-" + count.addAndGet(1L);
    }


    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(nextThreadName());
        return thread;
    }
}
